


//
//  NMyShenbaoViewController.m
//  nncloud
//
//  Created by macfai on 2018/11/22.
//  Copyright © 2018 macfai. All rights reserved.
//

#import "NMyShenbaoViewController.h"
#import "NMyShenbaoCell.h"
#import "NShenbaoTypeCell.h"//和申报类型共用一个cell
#import "NMyShenbaoModel.h"
#import "NMyShenbaoDetailViewController.h"

@interface NMyShenbaoViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)NSMutableArray *dataArray;

@property (nonatomic,strong)NSArray *titleArray;

@property (nonatomic,strong)NSArray *detailArray;

@end



@implementation NMyShenbaoViewController
#define cellIdentifier @"myshenbao"

-(UITableView *)tableView{
    
    if (!_tableView) {
        
        WeakStament(ws);
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight - 64-50) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 373.f;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"NMyShenbaoCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        //        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //                        [ws loadData];
        //        }];
        //        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //            [ws loadMoreData];
        //        }];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = [NSMutableArray array];
//    self.view.backgroundColor = kWhiteColor;
    //名字由我的申报变为   已申报项目
    [self.navigationItem setItemWithTitle:@"已申报项目" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    [self.view addSubview:self.tableView];
//    [self addFooter];
    self.titleArray = @[@"项目名称",@"项目起止年限",@"项目总投资(万元)",@"项目建设地点",@"申报日期",@"所属行业",@"所属科室",@"当前状态",];
    
    //先写假数据，后面要换成真的
    self.detailArray = @[@"南宁工业平台",@"2018-2019",@"1000000",@"广西",@"2018",@"互联网大数据",@"公安局",@"当前状态",];
    
    [self loadData];
    // Do any additional setup after loading the view.
}

-(void)addFooter {
    UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 100.f)];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 20, 300, 50);
    btn.centerX = footer.centerX;
    btn.backgroundColor = ThemeBlue;
    btn.layer.cornerRadius = 5.f;
    btn.layer.masksToBounds = YES;
    [btn setTitle:@"项目详情" forState:UIControlStateNormal];
//    [btn addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
    [footer addSubview:btn];
//    [self loadData];
}


- (void)loadData{
    
    //userId 企业端必填，服务端可以不填
    
    NSDictionary *dict = @{@"userId":[NUserModel shareInstance].userInfoId?:@""};
    NSInteger userType = [[NUserModel shareInstance].userInfoType integerValue];
    
    [[NNRCNetRequest manager]getWithUrl:kMyShenbaolistUrl params:(userType==1)?dict:@{} success:^(id result) {
        
        YYLog(@"%@",result);
        WeakStament(ws);
        if ([[result objectForKey:@"code"] integerValue] == 0) {
            NSArray *arr = result[@"page"][@"records"];
            YYLog(@"%@",arr);
            for (NSDictionary *dict in arr) {
                NMyShenbaoModel *model = [NMyShenbaoModel mj_objectWithKeyValues:dict];
                [ws.dataArray addObject:model];
            }
            [ws.tableView reloadData];

        }else{
            [AFToast showText:result[@"msg"]];
        }
        
    } failure:^(id failure) {
        
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
#pragma mark - datasource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
//    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NMyShenbaoModel *model = self.dataArray[indexPath.row];
    NMyShenbaoCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.model = model;
    [cell.detailBtn addTarget:self action:@selector(tapAciton:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

- (void)tapAciton:(UIButton *)btn
{
    UIView *contentView = (UIView *)[btn superview];
    NMyShenbaoCell *cell = (NMyShenbaoCell *)[contentView superview];
    NSIndexPath *myindexPath = [self.tableView indexPathForCell:cell];
    NMyShenbaoModel *model = self.dataArray[myindexPath.row];
    
    
    [NUserModel shareInstance].shenbaoId = model.declarationId;
    NMyShenbaoDetailViewController *de = [NMyShenbaoDetailViewController new];
    [self.navigationController pushViewController:de animated:YES];

    
}

@end
